package com.agm.android.fragments;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.agm.android.task.SyncTask;
import com.agm.dto.LegaSquadra;
import com.example.android.navigationdrawerexample.App;
import com.example.android.navigationdrawerexample.R;

public class LegaFragment extends Fragment {
	public static final String ARG_PLANET_NUMBER = "planet_number";
	private Fragment fragment = this;
	private int selected;
	public LegaFragment() {
		// Empty constructor required for fragment subclasses
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_lega, container,false);
		Spinner spinner = (Spinner)rootView.findViewById(R.id.lega_spinner);
		List<String> items = new ArrayList<String>();
		items.add("seleziona Lega");
		int index = 1;
		
		for(LegaSquadra lega : App.leghe){
			items.add(lega.getNome());
			if(App.lega != null){
				if(lega.getNome().equals(App.lega.getNome())){
					selected = index;
				}
			}
			index++;
		}
		//String[] items = new String[]{"1", "2", "three"};
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_spinner_item, items);
		spinner.setAdapter(adapter);
		spinner.setSelection(selected);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	if(position > 0){
			    	App.lega = App.leghe[position  - 1];
			    	SyncTask syncTask = new SyncTask();
			    	syncTask.execute((Void) null);
			    	if(selected != position){
				    	FragmentManager fragmentManager = getFragmentManager();
				    	fragment = new LegaFragment();
				    	fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
			    	}
		    	}
		    }

		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {
		        // your code here
		    }

		});
		if(App.lega != null){
			TextView nome = (TextView) rootView.findViewById(R.id.nomeLegaValue);
			TextView stato = (TextView) rootView.findViewById(R.id.statoLegaValue);
			TextView tipo = (TextView) rootView.findViewById(R.id.tipoLegaValue);
			TextView presidente = (TextView) rootView.findViewById(R.id.presidenteValue);
			TextView giornataCorrente = (TextView) rootView.findViewById(R.id.giornatacorrenteValue);
			TextView numeroSquadre = (TextView) rootView.findViewById(R.id.squadreiscritteValue);
			TextView nomesquadra = (TextView) rootView.findViewById(R.id.squadraValue);
			nome.setText(App.lega.getNome());
			stato.setText(App.lega.getStato());
			tipo.setText(App.lega.getTipo());
			presidente.setText(App.lega.getPresidente());
			giornataCorrente.setText("" + App.lega.getGiornatacorrente());
			numeroSquadre.setText("" + App.lega.getSquadreiscritte());
			nomesquadra.setText(App.lega.getNomesquadra());
			if(App.lega.getScudetto() != null && !"".equals(App.lega.getScudetto())){
			Bitmap bmp = BitmapFactory.decodeByteArray(App.lega.getScudetto(), 0, App.lega.getScudetto().length);
				ImageView image = (ImageView) rootView.findViewById(R.id.scudetto);
				image.setImageBitmap(bmp);
			}

		}
		if(App.utente != null){
			TextView nome = (TextView) rootView.findViewById(R.id.nomeValue);
			TextView cognome = (TextView) rootView.findViewById(R.id.cognomeValue);
			TextView username = (TextView) rootView.findViewById(R.id.usernameValue);
			TextView legaPrivata = (TextView) rootView.findViewById(R.id.legaprivataValue);
			TextView legaPubblica = (TextView) rootView.findViewById(R.id.legapubblicaValue);
			nome.setText(App.utente.getNome());
			cognome.setText(App.utente.getCognome());
			username.setText(App.utente.getUserid());
			
			legaPrivata.setText("" + App.utente.getIdlegaprivata());
			legaPubblica.setText("" + App.utente.getIdlegapubblica());
		}
		return rootView;
	}
}
